package com.slipper.ai.model.embedding.support;

import com.slipper.ai.model.embedding.EmbeddingModelBuilder;
import com.slipper.core.gson.GsonUtil;
import com.slipper.exception.CommonException;
import com.slipper.system.ai.bo.AiModelBO;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.ollama.OllamaEmbeddingModel;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.ai.ollama.management.ModelManagementOptions;

import java.util.Map;

public class OllamaEmbeddingModelBuilder implements EmbeddingModelBuilder {
    @Override
    public EmbeddingModel build(AiModelBO model) {
        Map<String, Object> options = GsonUtil.toMap(model.getParamJson());
        if (options == null || options.isEmpty() || !options.containsKey("apiUrl")) {
            throw new CommonException("参数配置不正确！");
        }

        String apiUrl = (String) options.get("apiUrl");
        return new OllamaEmbeddingModel(
                OllamaApi.builder().baseUrl(apiUrl).build(),
                OllamaOptions.builder().model(model.getName()).build(),
                ObservationRegistry.NOOP,
                ModelManagementOptions.defaults());
    }
}
